package com.keming.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.keming.pojo.Type;
import com.keming.service.Impl.TypeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class TypeController {
    @Autowired
    private TypeServiceImpl typeService;
    //分类查询
    @GetMapping("/types")
    public String type(Model model,
                       @RequestParam(defaultValue = "1" ,value = "pageNum")Integer pageNum){
        //按照排序字段 倒序 排序
        String orderBy = "id desc";
        PageHelper.startPage(pageNum,3,orderBy);
        List<Type> list = typeService.getAllType();
        PageInfo<Type> pageInfo = new PageInfo<Type>(list,3);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/types";
    }
    @GetMapping("/types-input")
    public String input(Model model){
        model.addAttribute("type",new Type());
        return "admin/types-input";
    }
    //增
    @PostMapping("/types")
    public String post(Type type, RedirectAttributes attributes){
        System.out.println("---types---");
        Type type1 = typeService.getTypeByName(type.getName());
        System.out.println(type1);
        if (type1!=null){
            attributes.addFlashAttribute("message","不能添加重复标签");
            return "redirect:/admin/types-input";
        }
        int i = typeService.saveType(type);
        if (i==0){
            attributes.addFlashAttribute("message","新增失败");
        }else {
            attributes.addFlashAttribute("message","新增成功");
        }
        return "redirect:/admin/types";
    }
//    跳转到修改页面
    @GetMapping("/types/{id}/input")
    public String editInput(@PathVariable Long id,Model model){
        model.addAttribute("type",typeService.getType(id));
        return "admin/types-input";
    }
//    修改页面跳转types
    @PostMapping("/types/{id}")
    public String editPost(Type type, RedirectAttributes attributes){
        Type type1 = typeService.getTypeByName(type.getName());
        if (type1!=null){
            attributes.addFlashAttribute("message","不能重复添加");
            return "redirect:/admin/types/input";
        }
        int t = typeService.updateType(type);
        if (t == 0 ) {
            attributes.addFlashAttribute("message", "编辑失败");
        } else {
            attributes.addFlashAttribute("message", "编辑成功");
        }
        return "redirect:/admin/types";

    }

//    删除
    @GetMapping("/types/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes){
        typeService.deleteType(id);
        attributes.addFlashAttribute("message","删除成功");
        System.out.println("删除成功");
        return "redirect:/admin/types";
    }



}
